package com.inei.evaluacionmype.controller.activity;

import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import com.inei.evaluacionmype.R;
import com.inei.evaluacionmype.controller.CapituloIController;
import com.inei.evaluacionmype.controller.CiiuController;
import com.inei.evaluacionmype.controller.ClanaeController;
import com.inei.evaluacionmype.controller.JustificacionController;
import com.inei.evaluacionmype.controller.MarcoController;
import com.inei.evaluacionmype.controller.TipoController;
import com.inei.evaluacionmype.controller.TipoViaController;
import com.inei.evaluacionmype.controller.UbigeoController;
import com.inei.evaluacionmype.model.entity.Cap01;
import com.inei.evaluacionmype.model.entity.Ciiu;
import com.inei.evaluacionmype.model.entity.Clanae;
import com.inei.evaluacionmype.model.entity.Dep;
import com.inei.evaluacionmype.model.entity.Dis;
import com.inei.evaluacionmype.model.entity.Justificacion;
import com.inei.evaluacionmype.model.entity.Marco;
import com.inei.evaluacionmype.model.entity.Pro;
import com.inei.evaluacionmype.model.entity.Tipo;
import com.inei.evaluacionmype.model.entity.TipoVia;
import com.inei.evaluacionmype.tools.Mensaje;

public class CapituloI_Activity extends Fragment {

	// componentes
	private View root;
	private EditText et01, et02, et03, et04, et05, et06, et08, et09, et10,
			et11, et12, et13, et14, et15, et16, et17, et18, et231, et232,
			et233, et234, et235;
	private Spinner sp07, sp191, sp192, sp193, sp20, sp21, sp22;

	// variables
	private CapituloIController con;
	private MarcoController con2;
	private Cap01 obj;
	private String ruc_id;
	private UbigeoController con3;
	private List<Dep> lDepa;
	private String ccdd;
	private List<Pro> lProv;
	private String ccpp;
	private List<Dis> lDist;
	private TipoController con4;
	private List<Tipo> lTip;
	private CiiuController con5;
	private List<Ciiu> lCiiu;
	private ClanaeController con6;
	private List<Clanae> lClan;
	private TipoViaController con7;
	private List<TipoVia> lTipVia;
	private JustificacionController con8;
	private List<Justificacion> lJust;
	private boolean nuevo = false;
	private boolean nuevoJust = false;
	private Mensaje mensaje;
	private int p;
	private int d;

	// cosntructor
	public CapituloI_Activity() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.activity_capitulo_i, container, false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		con = new CapituloIController(getActivity());
		con2 = new MarcoController(getActivity());
		con3 = new UbigeoController(getActivity());
		con4 = new TipoController(getActivity());
		con5 = new CiiuController(getActivity());
		con6 = new ClanaeController(getActivity());
		con7 = new TipoViaController(getActivity());
		con8 = new JustificacionController(getActivity());
		mensaje = new Mensaje(getActivity());

		d = 0;
		p = 0;

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	private void IniciarComponentes() {
		et01 = (EditText) root.findViewById(R.id.et01);
		et02 = (EditText) root.findViewById(R.id.et02);
		et03 = (EditText) root.findViewById(R.id.et03);
		et04 = (EditText) root.findViewById(R.id.et04);
		et05 = (EditText) root.findViewById(R.id.et05);
		et06 = (EditText) root.findViewById(R.id.et06);
		et08 = (EditText) root.findViewById(R.id.et08);
		et09 = (EditText) root.findViewById(R.id.et09);
		et10 = (EditText) root.findViewById(R.id.et10);
		et11 = (EditText) root.findViewById(R.id.et11);
		et12 = (EditText) root.findViewById(R.id.et12);
		et13 = (EditText) root.findViewById(R.id.et13);
		et14 = (EditText) root.findViewById(R.id.et14);
		et15 = (EditText) root.findViewById(R.id.et15);
		et16 = (EditText) root.findViewById(R.id.et16);
		et17 = (EditText) root.findViewById(R.id.et17);
		et18 = (EditText) root.findViewById(R.id.et18);
		et231 = (EditText) root.findViewById(R.id.et231);
		et232 = (EditText) root.findViewById(R.id.et232);
		et233 = (EditText) root.findViewById(R.id.et233);
		et234 = (EditText) root.findViewById(R.id.et234);
		et235 = (EditText) root.findViewById(R.id.et235);

		sp07 = (Spinner) root.findViewById(R.id.sp07);
		sp191 = (Spinner) root.findViewById(R.id.sp191);
		sp192 = (Spinner) root.findViewById(R.id.sp192);
		sp193 = (Spinner) root.findViewById(R.id.sp193);
		sp20 = (Spinner) root.findViewById(R.id.sp20);
		sp21 = (Spinner) root.findViewById(R.id.sp21);
		sp22 = (Spinner) root.findViewById(R.id.sp22);

		lTipVia = con7.TipoViaObtenerTodo();
		TipoVia tipovia = new TipoVia();
		tipovia.setCOD("0");
		tipovia.setDESCRIPCION("Seleccionar Tipo Via");
		tipovia.setABREVIATURA("Seleccionar");
		lTipVia.add(0, tipovia);
		sp07.setAdapter(new ArrayAdapter<TipoVia>(getActivity(),
				android.R.layout.simple_spinner_item, lTipVia));

		lDepa = con3.DepObtenerTodo();
		Dep dep = new Dep();
		dep.setCcdd("00");
		dep.setNom_dpto("Seleccionar Departamento");
		lDepa.add(0, dep);
		sp191.setAdapter(new ArrayAdapter<Dep>(getActivity(),
				android.R.layout.simple_spinner_item, lDepa));
		ccdd = "00";
		ccpp = "00";

		lCiiu = con5.CiiuObtenerTodo();
		Ciiu ciiu = new Ciiu();
		ciiu.setCIIU("0000");
		ciiu.setNOMCIIU("Seleccionar CIIU");
		lCiiu.add(0, ciiu);
		sp20.setAdapter(new ArrayAdapter<Ciiu>(getActivity(),
				android.R.layout.simple_spinner_item, lCiiu));

		lClan = con6.ClanaeObtenerTodo();
		Clanae clanae = new Clanae();
		clanae.setCLANAE("00000");
		clanae.setNOMCLANAE("Seleccionar CLANAE");
		lClan.add(0, clanae);
		sp21.setAdapter(new ArrayAdapter<Clanae>(getActivity(),
				android.R.layout.simple_spinner_item, lClan));

		lTip = con4.TipoObtenerTodo();
		Tipo tipo = new Tipo();
		tipo.setTIPOORG("00");
		tipo.setNOMTIPO("Seleccionar Tipo");
		lTip.add(0, tipo);
		sp22.setAdapter(new ArrayAdapter<Tipo>(getActivity(),
				android.R.layout.simple_spinner_item, lTip));
	}

	private void AgregarEventos() {
		sp191.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v, int pos,
					long id) {
				Pro pro = new Pro();
				pro.setCcdd("00");
				pro.setCcpp("00");
				pro.setNom_prov("Seleccionar Provincia");
				if (pos != 0) {
					ccdd = sp191.getItemAtPosition(pos).toString()
							.substring(0, 2);
					lProv = con3.ProObtenerXDep(ccdd);
					lProv.add(0, pro);
					sp192.setAdapter(new ArrayAdapter<Pro>(getActivity(),
							android.R.layout.simple_spinner_item, lProv));
					if (obj != null && p == 0) {
						for (int i = 0; i < lProv.size(); i++) {
							if (obj.getUBIGEO().substring(2, 4)
									.equals(lProv.get(i).getCcpp())) {
								sp192.setSelection(i);
								break;
							}
						}
						p = 1;
					}
				} else {
					lProv = new ArrayList<Pro>();
					lProv.add(0, pro);
					sp192.setAdapter(new ArrayAdapter<Pro>(getActivity(),
							android.R.layout.simple_spinner_item, lProv));
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		sp192.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v, int pos,
					long id) {
				Dis dis = new Dis();
				dis.setCcdd("00");
				dis.setCcpp("00");
				dis.setCcdi("00");
				dis.setNom_dist("Seleccionar Distrito");
				if (pos != 0) {
					ccpp = sp192.getItemAtPosition(pos).toString()
							.substring(0, 2);
					lDist = con3.DisObtenerXDepXPro(ccdd, ccpp);
					lDist.add(0, dis);
					sp193.setAdapter(new ArrayAdapter<Dis>(getActivity(),
							android.R.layout.simple_spinner_item, lDist));
					if (obj != null && d == 0) {
						for (int i = 0; i < lDist.size(); i++) {
							if (obj.getUBIGEO()
									.substring(4)
									.equals(lDist.get(i).toString()
											.substring(0, 2))) {
								sp193.setSelection(i);
								break;
							}
						}
						d = 1;
					}
				} else {
					lDist = new ArrayList<Dis>();
					lDist.add(0, dis);
					sp193.setAdapter(new ArrayAdapter<Dis>(getActivity(),
							android.R.layout.simple_spinner_item, lDist));
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	private void CargarDatos() {
		ruc_id = getArguments().getString("ruc_id");
		obj = new Cap01();
		obj.setRUC(ruc_id);

		obj = con.Cap01Obtener(obj);
		if (obj == null) {
			nuevo = true;
			Marco tmp = new Marco();
			tmp.setRUC(ruc_id);
			tmp = con2.MarcoObtener(tmp);

			obj = new Cap01();
			obj.setRUC(tmp.getRUC());
			if (tmp.getRAZON_SOCIAL() != null) {
				obj.setC1P2RASO(tmp.getRAZON_SOCIAL());
			}
			if (tmp.getNOMBRE_COMERCIAL() != null) {
				obj.setC1P3NOMCOMER(tmp.getNOMBRE_COMERCIAL());
			}
			if (tmp.getTELEFONO() != null) {
				obj.setC1P4NUMTELEF1(tmp.getTELEFONO());
			}
			if (tmp.getTELEFONO1() != null) {
				obj.setC1P5NUMTELEF2(tmp.getTELEFONO1());
			}
			obj.setC1P6FAX(tmp.getFAX() == 0 ? "" : (tmp.getFAX() + ""));
			if (tmp.getDESC_TIPO_VIA() != null) {
				obj.setC1P7TIPOVIA(tmp.getDESC_TIPO_VIA());
			}
			if (tmp.getNOMBRE_VIA() != null) {
				obj.setC1P8DOMICILIO(tmp.getNOMBRE_VIA());
			}
			if (tmp.getPUERTA() != null) {
				obj.setC1P9NRO(tmp.getPUERTA());
			}
			if (tmp.getINTERIOR() != null) {
				obj.setC1P10INT(tmp.getINTERIOR());
			}
			if (tmp.getPISO() != null) {
				obj.setC1P11PISO(tmp.getPISO());
			}
			if (tmp.getMANZANA() != null) {
				obj.setC1P12MZN(tmp.getMANZANA());
			}
			if (tmp.getLOTE() != null) {
				obj.setC1P13LOTE(tmp.getLOTE());
			}
			if (tmp.getKILOMETRO() != null) {
				obj.setC1P14KM(tmp.getKILOMETRO().equals("") ? 0.0 : (Double
						.parseDouble(tmp.getKILOMETRO())));
			}
			if (tmp.getREFERENCIA() != null) {
				obj.setC1P15REFERENCIA(tmp.getREFERENCIA());
			}
			if (tmp.getANIO_APERTURA() != null) {
				obj.setC1P16ANIO_INI(tmp.getANIO_APERTURA());
			}
			if (tmp.getEMAIL() != null) {
				obj.setC1P17EMAIL(tmp.getEMAIL());
			}
			if (tmp.getUBIGEO() != null) {
				obj.setUBIGEO(tmp.getUBIGEO());
			}
			if (tmp.getDEPT() != null) {
				obj.setC1P19A_NOMCCDD(tmp.getDEPT());
			}
			if (tmp.getPROV() != null) {
				obj.setC1P19B_NOMCCPP(tmp.getPROV());
			}
			if (tmp.getDIST() != null) {
				obj.setC1P19C_NOMCCDI(tmp.getDIST());
			}
			if (tmp.getCIIU_V4() != null) {
				obj.setCIIU_ID(tmp.getCIIU_V4());
			}
			if (tmp.getORGANIZACION_JURIDICA() != null) {
				obj.setC1P22TIPOORG(tmp.getORGANIZACION_JURIDICA().substring(0,
						2));
			}
		}

		if (obj != null) {
			et01.setText(obj.getRUC());
			et02.setText(obj.getC1P2RASO());
			et03.setText(obj.getC1P3NOMCOMER());
			et04.setText(obj.getC1P4NUMTELEF1());
			et05.setText(obj.getC1P5NUMTELEF2());
			et06.setText(obj.getC1P6FAX());
			if (obj.getC1P7TIPOVIA() != null) {
				for (int i = 0; i < lTipVia.size(); i++) {
					if (obj.getC1P7TIPOVIA().equals(lTipVia.get(i).getCOD())) {
						sp07.setSelection(i);
						break;
					}
				}
			}
			et08.setText(obj.getC1P8DOMICILIO());
			et09.setText(obj.getC1P9NRO());
			et10.setText(obj.getC1P10INT());
			et11.setText(obj.getC1P11PISO());
			et12.setText(obj.getC1P12MZN());
			et13.setText(obj.getC1P13LOTE());
			et14.setText(obj.getC1P14KM() + "");
			et15.setText(obj.getC1P15REFERENCIA());
			et16.setText(obj.getC1P16ANIO_INI());
			et17.setText(obj.getC1P17EMAIL());
			et18.setText(obj.getC1P18WEB());
			if (obj.getUBIGEO() != null) {
				for (int i = 0; i < lDepa.size(); i++) {
					if (obj.getUBIGEO().substring(0, 2)
							.equals(lDepa.get(i).getCcdd())) {
						sp191.setSelection(i);
						break;
					}
				}
			}
			if (obj.getCIIU_ID() != null) {
				for (int i = 0; i < lCiiu.size(); i++) {
					if (obj.getCIIU_ID().equals(lCiiu.get(i).getCIIU())) {
						sp20.setSelection(i);
						break;
					}
				}
			}
			if (obj.getC1P21CLANAE() != null) {
				for (int i = 0; i < lClan.size(); i++) {
					if (obj.getC1P21CLANAE().equals(lClan.get(i).getCLANAE())) {
						sp21.setSelection(i);
						break;
					}
				}
			}
			if (obj.getC1P22TIPOORG() != null) {
				for (int i = 0; i < lTip.size(); i++) {
					if (obj.getC1P22TIPOORG().equals(lTip.get(i).getTIPOORG())) {
						sp22.setSelection(i);
						break;
					}
				}
			}
			et231.setText(obj.getC1P23A_TELEFOFIJO());
			et232.setText(obj.getC1P23B_NOMBRES());
			et233.setText(obj.getC1P23C_CARGO());
			et234.setText(obj.getC1P23D_MOVIL());
			et235.setText(obj.getC1P23E_EMAIL());
		}

		lJust = con8.JustificacionObtenerPorRuc(ruc_id, "1");
		if (lJust == null) {
			nuevoJust = true;
			lJust = new ArrayList<Justificacion>();
			for (int i = 0; i < 22; i++) {
				Justificacion just = new Justificacion();
				just.setRUC(ruc_id);
				just.setCAPITULO("1");
				just.setCLAVE(i + "");
				just.setFLAG("1");
				lJust.add(i, just);
			}
			lJust.get(0).setREGLA("1. El RUC DEBE CONTENER 11 DIGITOS");
			lJust.get(1).setREGLA("2. DIFERENTE DE VACIO");
			lJust.get(2).setREGLA("4. DIFERENTE DE VACIO");
			lJust.get(3).setREGLA("4. MIN 6 CARACTERES MAX 9 CARACTERES");
			lJust.get(4).setREGLA("5. MIN 6 CARACTERES MAX 9 CARACTERES");
			lJust.get(5).setREGLA("6. MIN 6 CARACTERES MAX 9 CARACTERES");
			lJust.get(6).setREGLA("7. DIFERENTE DE VACIO");
			lJust.get(7).setREGLA("8. DIFERENTE DE VACIO");
			lJust.get(8).setREGLA("11. DIFERENTE DE VACIO");
			lJust.get(9).setREGLA("15. DIFERENTE DE VACIO");
			lJust.get(10).setREGLA("16. DIFERENTE DE VACIO");
			lJust.get(11).setREGLA("16. MENOR O IGUAL QUE 2013");
			lJust.get(12).setREGLA("19A. DIFERENTE DE VACIO");
			lJust.get(13).setREGLA("19B. DIFERENTE DE VACIO");
			lJust.get(14).setREGLA("19C. DIFERENTE DE VACIO");
			lJust.get(15).setREGLA("20. DIFERENTE DE VACIO");
			lJust.get(16).setREGLA("21. DIFERENTE DE VACIO");
			lJust.get(17).setREGLA("22. DIFERENTE DE VACIO");
			lJust.get(18)
					.setREGLA("23A. si P3B <> VACIO entonces P3A <> VACIO");
			lJust.get(19)
					.setREGLA("23C. si P3B <> VACIO entonces P3C <> VACIO");
			lJust.get(20)
					.setREGLA("23D. si P3B <> VACIO entonces P3D <> VACIO");
			lJust.get(21)
					.setREGLA("23E. si P3B <> VACIO entonces P3E <> VACIO");
		}
	}

	public void ValidarCap() {
		if (et01.getText().toString().trim().length() != 11) {
			et01.requestFocus();
			et01.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			et01.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et02.getText().toString().trim().length() == 0) {
			et02.requestFocus();
			et02.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			et02.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et04.getText().toString().trim().length() == 0) {
			et04.setBackgroundResource(R.drawable.cap01_celda_error);
			et04.requestFocus();
		} else {
			if (et04.getText().toString().trim().length() < 6) {
				et04.setBackgroundResource(R.drawable.cap01_celda_error);
				et04.requestFocus();
			} else {
				et04.setBackgroundResource(R.drawable.cap01_celda);
			}
		}
		if (et05.getText().toString().trim().length() != 0
				&& et05.getText().toString().trim().length() < 6) {
			et05.setBackgroundResource(R.drawable.cap01_celda_error);
			et05.requestFocus();
		} else {
			et05.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et06.getText().toString().trim().length() != 0
				&& et06.getText().toString().trim().length() < 6) {
			et06.setBackgroundResource(R.drawable.cap01_celda_error);
			et06.requestFocus();
		} else {
			et06.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (sp07.getSelectedItemPosition() == 0) {
			sp07.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp07.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et08.getText().toString().trim().length() == 0) {
			et08.setBackgroundResource(R.drawable.cap01_celda_error);
			et08.requestFocus();
		} else {
			et08.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et11.getText().toString().trim().length() == 0) {
			et11.setBackgroundResource(R.drawable.cap01_celda_error);
			et11.requestFocus();
		} else {
			et11.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et15.getText().toString().trim().length() == 0) {
			et15.setBackgroundResource(R.drawable.cap01_celda_error);
			et15.requestFocus();
		} else {
			et15.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et16.getText().toString().trim().length() == 0) {
			et16.setBackgroundResource(R.drawable.cap01_celda_error);
			et16.requestFocus();
		} else {
			if (Integer.parseInt(et16.getText().toString()) > 2013) {
				et16.setBackgroundResource(R.drawable.cap01_celda_error);
				et16.requestFocus();
			} else {
				et16.setBackgroundResource(R.drawable.cap01_celda);
			}
		}
		if (sp191.getSelectedItemPosition() == 0) {
			sp191.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp191.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (sp192.getSelectedItemPosition() == 0) {
			sp192.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp192.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (sp193.getSelectedItemPosition() == 0) {
			sp193.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp193.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (sp20.getSelectedItemPosition() == 0) {
			sp20.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp20.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (sp21.getSelectedItemPosition() == 0) {
			sp21.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp21.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (sp22.getSelectedItemPosition() == 0) {
			sp22.setBackgroundResource(R.drawable.cap01_celda_error);
		} else {
			sp22.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et232.getText().toString().trim().length() != 0
				&& et231.getText().toString().trim().length() == 0) {
			et231.setBackgroundResource(R.drawable.cap01_celda_error);
			et231.requestFocus();
		} else {
			et231.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et232.getText().toString().trim().length() != 0
				&& et233.getText().toString().trim().length() == 0) {
			et233.setBackgroundResource(R.drawable.cap01_celda_error);
			et233.requestFocus();
		} else {
			et233.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et232.getText().toString().trim().length() != 0
				&& et234.getText().toString().trim().length() == 0) {
			et234.setBackgroundResource(R.drawable.cap01_celda_error);
			et234.requestFocus();
		} else {
			et234.setBackgroundResource(R.drawable.cap01_celda);
		}
		if (et232.getText().toString().trim().length() != 0
				&& et235.getText().toString().trim().length() == 0) {
			et235.setBackgroundResource(R.drawable.cap01_celda_error);
			et235.requestFocus();
		} else {
			et235.setBackgroundResource(R.drawable.cap01_celda);
		}
	}

	public void GuardarCap() {
		obj.setRUC(et01.getText().toString());
		obj.setC1P2RASO(et02.getText().toString());
		obj.setC1P3NOMCOMER(et03.getText().toString());
		obj.setC1P4NUMTELEF1(et04.getText().toString());
		obj.setC1P5NUMTELEF2(et05.getText().toString());
		obj.setC1P6FAX(et06.getText().toString());
		obj.setC1P8DOMICILIO(et08.getText().toString());
		obj.setC1P9NRO(et09.getText().toString());
		obj.setC1P10INT(et10.getText().toString());
		obj.setC1P11PISO(et11.getText().toString());
		obj.setC1P12MZN(et12.getText().toString());
		obj.setC1P13LOTE(et13.getText().toString());
		obj.setC1P14KM(et14.getText().toString().trim().length() == 0 ? 0
				: (Double.parseDouble(et14.getText().toString())));
		obj.setC1P15REFERENCIA(et15.getText().toString());
		obj.setC1P16ANIO_INI(et16.getText().toString());
		obj.setC1P17EMAIL(et17.getText().toString());
		obj.setC1P18WEB(et18.getText().toString());
		obj.setC1P23A_TELEFOFIJO(et231.getText().toString());
		obj.setC1P23B_NOMBRES(et232.getText().toString());
		obj.setC1P23C_CARGO(et233.getText().toString());
		obj.setC1P23D_MOVIL(et234.getText().toString());
		obj.setC1P23E_EMAIL(et235.getText().toString());

		obj.setC1P7TIPOVIA(((TipoVia) sp07.getSelectedItem()).getCOD());
		obj.setC1P19A_NOMCCDD(((Dep) sp191.getSelectedItem()).getNom_dpto());
		obj.setC1P19B_NOMCCPP(((Pro) sp192.getSelectedItem()).getNom_prov());
		obj.setC1P19C_NOMCCDI(((Dis) sp193.getSelectedItem()).getNom_dist());
		obj.setUBIGEO(((Dep) sp191.getSelectedItem()).getCcdd()
				+ ((Pro) sp192.getSelectedItem()).getCcpp()
				+ ((Dis) sp193.getSelectedItem()).getCcdi());
		obj.setCIIU_ID(((Ciiu) sp20.getSelectedItem()).getCIIU());
		obj.setC1P21CLANAE(((Clanae) sp21.getSelectedItem()).getCLANAE());
		obj.setC1P22TIPOORG(((Tipo) sp22.getSelectedItem()).getTIPOORG());

		if (nuevo) {
			if (con.Cap01Insertar(obj) > 0) {
				nuevo = false;
				ValidarJust();
				mensaje.Exito("SE GRABO CON EXITO");

			} else {
				mensaje.Error("ERROR AL GRABAR");
			}
		} else {
			if (con.Cap01Modificar(obj) > 0) {
				ValidarJust();
				mensaje.Exito("SE MODIFICO CON EXITO");
			} else {
				mensaje.Error("ERROR AL MODIFICAR");
			}
		}
	}

	private void ValidarJust() {
		if (et01.getText().toString().trim().length() != 11) {
			lJust.get(0).setFLAG("0");
		} else {
			lJust.get(0).setFLAG("1");
		}
		if (et02.getText().toString().trim().length() == 0) {
			lJust.get(1).setFLAG("0");
		} else {
			lJust.get(1).setFLAG("1");
		}
		if (et04.getText().toString().trim().length() == 0) {
			lJust.get(2).setFLAG("0");
		} else {
			lJust.get(2).setFLAG("1");
		}
		if (et04.getText().toString().trim().length() < 6) {
			lJust.get(3).setFLAG("0");
		} else {
			lJust.get(3).setFLAG("1");
		}
		if (et05.getText().toString().trim().length() != 0
				&& et05.getText().toString().trim().length() < 6) {
			lJust.get(4).setFLAG("0");
		} else {
			lJust.get(4).setFLAG("1");
		}
		if (et06.getText().toString().trim().length() != 0
				&& et06.getText().toString().trim().length() < 6) {
			lJust.get(5).setFLAG("0");
		} else {
			lJust.get(5).setFLAG("1");
		}
		if (sp07.getSelectedItemPosition() == 0) {
			lJust.get(6).setFLAG("0");
		} else {
			lJust.get(6).setFLAG("1");
		}
		if (et08.getText().toString().trim().length() == 0) {
			lJust.get(7).setFLAG("0");
		} else {
			lJust.get(7).setFLAG("1");
		}
		if (et11.getText().toString().trim().length() == 0) {
			lJust.get(8).setFLAG("0");
		} else {
			lJust.get(8).setFLAG("1");
		}
		if (et15.getText().toString().trim().length() == 0) {
			lJust.get(9).setFLAG("0");
		} else {
			lJust.get(9).setFLAG("1");
		}
		if (et16.getText().toString().trim().length() == 0) {
			lJust.get(10).setFLAG("0");
		} else {
			lJust.get(10).setFLAG("1");
		}
		if (Integer.parseInt(et16.getText().toString()) > 2013) {
			lJust.get(11).setFLAG("0");
		} else {
			lJust.get(11).setFLAG("1");
		}
		if (sp191.getSelectedItemPosition() == 0) {
			lJust.get(12).setFLAG("0");
		} else {
			lJust.get(12).setFLAG("1");
		}
		if (sp192.getSelectedItemPosition() == 0) {
			lJust.get(13).setFLAG("0");
		} else {
			lJust.get(13).setFLAG("1");
		}
		if (sp193.getSelectedItemPosition() == 0) {
			lJust.get(14).setFLAG("0");
		} else {
			lJust.get(14).setFLAG("1");
		}
		if (sp20.getSelectedItemPosition() == 0) {
			lJust.get(15).setFLAG("0");
		} else {
			lJust.get(15).setFLAG("1");
		}
		if (sp21.getSelectedItemPosition() == 0) {
			lJust.get(16).setFLAG("0");
		} else {
			lJust.get(16).setFLAG("1");
		}
		if (sp22.getSelectedItemPosition() == 0) {
			lJust.get(17).setFLAG("0");
		} else {
			lJust.get(17).setFLAG("1");
		}
		if (et232.getText().toString().trim().length() != 0
				&& et231.getText().toString().trim().length() == 0) {
			lJust.get(18).setFLAG("0");
		} else {
			lJust.get(18).setFLAG("1");
		}
		if (et232.getText().toString().trim().length() != 0
				&& et233.getText().toString().trim().length() == 0) {
			lJust.get(19).setFLAG("0");
		} else {
			lJust.get(19).setFLAG("1");
		}
		if (et232.getText().toString().trim().length() != 0
				&& et234.getText().toString().trim().length() == 0) {
			lJust.get(20).setFLAG("0");
		} else {
			lJust.get(20).setFLAG("1");
		}
		if (et232.getText().toString().trim().length() != 0
				&& et235.getText().toString().trim().length() == 0) {
			lJust.get(21).setFLAG("0");
		} else {
			lJust.get(21).setFLAG("1");
		}

		if (nuevoJust) {
			for (int i = 0; i < 22; i++) {
				con8.JustificacionInsertar(lJust.get(i));
			}
			nuevoJust = false;
		} else {
			for (int i = 0; i < 22; i++) {
				con8.JustificacionModificar(lJust.get(i));
			}
		}
	}
}
